﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BooksManagermentSystem
{
    public partial class ReturnBooks_form : Form
    {
        public ReturnBooks_form()
        {
            InitializeComponent();
        }

        private void ReturnBooks_form_Load(object sender, EventArgs e)
        {
            textBox4.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string bookid = textBox1.Text.Trim();
            string stuid = textBox3.Text.Trim();
            if(bookid == "")
            {
                MessageBox.Show("请输入书籍编号！");
                textBox1.Focus();
                return;
            }
            if (stuid == "")
            {
                MessageBox.Show("请输入学号！");
                textBox3.Focus();
                return;
            }

            float free = float.Parse(textBox5.Text);

            //完善借阅信息
            string sql = $"update RetrieveBook set endtime=getdate(),free = {free} where stuid = '{stuid}' and bookid = '{bookid}'";
            bool a = DBhelper.NonQuery(sql);

            //修改已借阅图书数量
            string sql2 = $"update BookInfo set useamount = useamount-1 where bookid='{bookid}'";
            bool b = DBhelper.NonQuery(sql2);

            //修改学生欠费
            string sql3 = $"update StuInfo set arrearage = arrearage+{free} where stuid = '{stuid}'";
            bool c = DBhelper.NonQuery(sql3);

            if(a&&b&&c)
            {
                MessageBox.Show("还书成功！");
            }
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox5.Clear();
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            string bookid = textBox1.Text.Trim();
            if(bookid == "")
            {
                return;
            }
            string sql = $"select bookname from BookInfo where bookid = '{bookid}'";
            DataRow dr = DBhelper.GetDataRow(sql);
            if(dr != null )
            {
                textBox2.Text = dr["bookname"].ToString();
            }
            else
            {
                MessageBox.Show("没有此书籍！");
                textBox1.Focus();
                return;
            }
        }

        private void textBox3_Leave(object sender, EventArgs e)
        {
            string stuid = textBox3.Text.Trim();
            if (stuid == "")
            {
                return;
            }
            string bookid = textBox1.Text.Trim();
            if (bookid == "")
            {
                return;
            }
            if (!DBhelper.checkStu(stuid))
            {
                MessageBox.Show("该学生不存在或未注册！");
                textBox3.Focus();
                return;
            }
            
            string sql = $"select * from RetrieveBook where bookid='{bookid}' and stuid = '{stuid}'";
            if(DBhelper.GetDataRow(sql) == null)
            {
                MessageBox.Show("该学生没有借此书");
                textBox3.Focus();
                return;
            }
            else
            {
                textBox5.Text = DBhelper.GetFree(bookid,stuid).ToString();
            }
        }
    }
}
